<?php

class SmsHelper
{
	const API_URL = 'http://api.smsbao.com/sms?u=%s&p=%s&m=%s&c=%s';
	const USERNAME = 'chinacv';
	const PASSWORD = 'chinacv123';
	
	public static function get_instance()
	{
		static $_instance;
		if (is_object($_instance)) {
			return $_instance;
		}
		$_instance = new SmsHelper();
		return $_instance;
	}
	
	public function send($user_number, $content)
	{
		$ret = false;
		if (empty($user_number)) {
			return $ret;
		}
	
		$url = sprintf(self::API_URL, self::USERNAME, md5(self::PASSWORD), $user_number, urlencode('开拍网验证码: ' . $content));
		$ch = curl_init($url);
		$options = array(
			CURLOPT_RETURNTRANSFER => true
		);
		curl_setopt_array($ch, $options);
		$response = curl_exec($ch);
		curl_close($ch);
		if ($response == 0 && $response !== false) {
			$ret = true;
			//写入缓存 有效期60秒
			Yii::app()->cache->set($user_number, $content, 60);
		}
		return $ret;
	}
	
	/**
	 * 随机生成验证码
	 */
	public function create_code()
	{
		$code = mt_rand(100000, 999999);
		return $code;
	}
}